class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :force_utf8_params
  helper_method :current_user_session, :current_user, :require_admin, :require_moderator, :require_operator,
    :set_form_settings, :set_default_setting, :init
  private

  def force_utf8_params
    @page = {}
    @page["name"] = "#{params[:controller]}.#{params[:action]}"
    @site = SITE
    @current_roles = []
    @current_roles = current_user.get_roles if current_user
    set_default_setting
    traverse = lambda do |object, block|
      if object.kind_of?(Hash)
        object.each_value { |o| traverse.call(o, block) }
      elsif object.kind_of?(Array)
        object.each { |o| traverse.call(o, block) }
      else
        block.call(object)
      end
      object
    end
    force_encoding = lambda do |o|
      o.force_encoding(Encoding::UTF_8) if o.respond_to?(:force_encoding)
    end
    traverse.call(params, force_encoding)
  end

  def init
    time = params[:time] || "day"
    town = params[:town] || "lviv"
    @time = ["day", "night"].include?(time) ? time : "day"
    @towns = Town.all
    town_keywords = @towns.map do |item| item.keyword end
    @town = town_keywords.include?(town) ? town : "lviv"
    @town_entity = @towns.select {|town| town.keyword == @town}.first
    @townShops = Shop.joins(:town).where("shops.status = 'approved' and towns.keyword=?",@town)
    @top_banners, @left_banners, @right_banners = [], [], []
    @banners = Banner.find(:all, :order => :orderby)
    @banners.each do |banner|
      @top_banners << banner if banner.position == "top"
      @left_banners << banner if banner.position == "left"
      @right_banners << banner if banner.position == "right"
    end
    @hot_novations = Novation.find(:all, :order => :created_at, :limit => 5).map do |item| item.title end
    @hot_receipts = Receipt.find(:all, :order => :created_at, :limit => 5).map do |item| item.title end
    @total = find_cart.get_total
  end

  def set_default_setting
    @page["scripts"] = ["jquery-1.5.2.min","jquery.png","rails", "s3SliderPacked", "roundmenu", "cart"]
    @page["styles"] = ["roundmenu"]
    @page["meta"] = true
    @page["bonus_title"] = nil
    @page["success"] = true
  end

  def set_form_settings
    @page["styles"] = ["roundmenuUI", "jquery.cleditor"]
    #@page["scripts"] = ["jquery-1.5.2.min", "jquery.cleditor.min", "jquery.cleditor.icon.min", "form"]
    @page["scripts"] = ["jquery-1.5.2.min","jquery.png", "tiny_mce/jquery.tinymce", "form"]
    @page["meta"] = false
  end

  def set_moderator_settings
    @page["styles"] = ["roundmenuAdmin"]
    @page["scripts"] = ["jquery-1.5.2.min", "jquery.png","rails"]
    @page["name"] = "site.cms.moderator"
    @page["meta"] = false
  end

  def set_admin_settings
    @page["styles"] = ["roundmenuAdmin"]
    @page["scripts"] = ["jquery-1.5.2.min", "jquery.png","rails"]
    @page["name"] = "site.cms.admin"
    @page["meta"] = false
  end

  def set_operator_settings
    @page["styles"] = ["roundmenuAdmin"]
    @page["scripts"] = ["jquery-1.5.2.min","jquery.png", "rails"]
    @page["name"] = "site.cms.operator"
    @page["meta"] = false
  end

  def current_user_session
    logger.debug "ApplicationController::current_user_session"
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    logger.debug "ApplicationController::current_user"
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end

  def require_user
    logger.debug "ApplicationController::require_user"
    unless current_user
      store_location
      flash[:notice] = I18n.t "site.message.require_user"
      redirect_to login_path(:time => @time, :town => @town)
      return false
    end
    true
  end

  def require_no_user
    logger.debug "ApplicationController::require_no_user"
    if current_user
      store_location
      flash[:notice] = I18n.t "site.message.require_no_user"
      redirect_to root_path
      return false
    end
  end

  def require_moderator
    logger.debug "ApplicationController::require_moderator"
    unless current_user && current_user.get_roles.include?("moderator")
      store_location
      flash[:notice] = I18n.t "site.message.require_moderator"
      redirect_to root_path
      return false
    end
  end

  def require_operator
    logger.debug "ApplicationController::require_operator"
    unless current_user && current_user.get_roles.include?("operator")
      store_location
      flash[:notice] = I18n.t "site.message.require_operator"
      redirect_to root_path
      return false
    end
  end

  def require_admin
    logger.debug "ApplicationController::require_admin"
    unless current_user && current_user.get_roles.include?("admin")
      store_location
      flash[:notice] = I18n.t "site.message.require_admin"
      redirect_to root_path
      return false
    end
  end

  def store_location
    session[:return_to] = request.request_uri
  end

  def redirect_back_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end

  def find_cart
    session[:cart] ||= Cart.new
  end

end
